// component/ui/z-tracks/z-tracks.js
import {
  _toArray
} from '../common/utils';
const path = '../z-track/z-track'

Component({
  relations: {
    [path]: {
      type: 'descendant',
      linked: function (target) {
        this.updateChild(target)
      },
      linkChanged: function (target) {},
      unlinked: function (target) {}
    }
  },

  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/,
  },

  /**
   * 组件的属性列表
   */
  properties: {
    value: {
      type: [String, Number],
      observer(val) {
        this.updateChildren(val)
      },
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    current: '',
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    created() {
      Object.defineProperty(this, 'children', {
        get: () => this.getRelationNodes(path) || [],
      });
    },
    attached() {},
    detached() {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    updateChild(child) {
      child.updateActive()
    },

    updateChildren(val) {
      this.setData({
        current: String(val),
      });
      (this.children || []).forEach((child) => child.updateActive())
    },
  }

})